package cn.iocoder.yudao.module.resource.service.material;

import cn.iocoder.yudao.module.resource.dal.dataobject.material.ResourceMaterialDO;
import cn.iocoder.yudao.module.resource.dal.mysql.material.ResourceMaterialMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

/**
 * 资源素材 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
@Slf4j
public class ResourceMaterialServiceImpl implements ResourceMaterialService {

    @Resource
    private ResourceMaterialMapper materialMapper;

    @Override
    public Long createMaterial(ResourceMaterialDO materialDO) {
        materialMapper.insert(materialDO);
        return materialDO.getId();
    }

    @Override
    public void updateMaterial(ResourceMaterialDO materialDO) {
        materialMapper.updateById(materialDO);
    }

    @Override
    public void deleteMaterial(Long id) {
        materialMapper.deleteById(id);
    }

    @Override
    public ResourceMaterialDO getMaterial(Long id) {
        return materialMapper.selectById(id);
    }

    @Override
    public List<ResourceMaterialDO> getMaterialList(List<Long> ids) {
        return materialMapper.selectBatchIds(ids);
    }

    @Override
    public List<ResourceMaterialDO> getMaterialListByCreatorId(Long creatorId) {
        return materialMapper.selectList(ResourceMaterialDO::getCreatorId, creatorId);
    }

    @Override
    public void auditMaterial(Long id, Integer status, String auditRemark) {
        ResourceMaterialDO updateObj = new ResourceMaterialDO();
        updateObj.setId(id);
        updateObj.setStatus(status);
        updateObj.setAuditRemark(auditRemark);
        materialMapper.updateById(updateObj);
    }

    @Override
    public void incrementDownloadCount(Long id) {
        ResourceMaterialDO material = materialMapper.selectById(id);
        if (material != null) {
            ResourceMaterialDO updateObj = new ResourceMaterialDO();
            updateObj.setId(id);
            updateObj.setDownloadCount(material.getDownloadCount() + 1);
            materialMapper.updateById(updateObj);
        }
    }

    @Override
    public void incrementCollectCount(Long id) {
        ResourceMaterialDO material = materialMapper.selectById(id);
        if (material != null) {
            ResourceMaterialDO updateObj = new ResourceMaterialDO();
            updateObj.setId(id);
            updateObj.setCollectCount(material.getCollectCount() + 1);
            materialMapper.updateById(updateObj);
        }
    }

    @Override
    public void incrementViewCount(Long id) {
        ResourceMaterialDO material = materialMapper.selectById(id);
        if (material != null) {
            ResourceMaterialDO updateObj = new ResourceMaterialDO();
            updateObj.setId(id);
            updateObj.setViewCount(material.getViewCount() + 1);
            materialMapper.updateById(updateObj);
        }
    }

}
